package cn.aigoubuy.dao.ilpl;
import cn.aigoubuy.dao.UserDao;
import cn.aigoubuy.domain.User;
import cn.aigoubuy.utils.JDBCUtils;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class UserDaoImpl implements UserDao {
    @Override
    public void save(User user) throws SQLException {
        String sql="INSERT INTO USER VALUES(?,?,?,?,?,?,?,?,?,?)";
        QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
        Object[] params=
                {user.getUid(),user.getUsername(),user.getPassword(),user.getName(),user.getEmail(),
                        user.getTelephone(),user.getBirthday(),user.getSex(),user.getState(),user.getCode()};
        Connection conn = JDBCUtils.getConnection();
        qr.execute(sql, params);
        /*Db.use().insert(
                Entity.create("user")
                        .set("uid", user.getUid())
                        .set("username", user.getUsername())
                        .set("password", user.getPassword())
                        .set("name", user.getName())
                        .set("email", user.getEmail())
                        .set("telephone", user.getTelephone())
                        .set("birthday", user.getBirthday())
                        .set("sex", user.getSex())
                        .set("state", user.getState())
                        .set("code", user.getCode())
        );*/
    }

    @Override
    public User find(String username, String password) throws SQLException {
        String sql="select * from user where username=? and password= ? and state = 1";
        QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
        return qr.query(sql, new BeanHandler<>
                (User.class),username,password);
        /*List<Entity> entities = Db.use().findAll(Entity.create("user").set("username", username).set("password", password));
        if (entities.size()==0||entities==null){
            return null;
        }else{
            return entities.get(0).toBean(User.class);
        }*/
    }

    @Override
    public void updateUser(User user) throws SQLException {
        String sql="update user set username=? , password=? ,name =?,email=?, telephone =? ,birthday =? ,sex=? ,state=? ,code= ? where uid=?";
        QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
        Object[] params=
                {user.getUsername(),user.getPassword(),user.getName(),
                        user.getEmail(),user.getTelephone(),user.getBirthday(),
                        user.getSex(),user.getState(),user.getCode(),user.getUid()};
        qr.update(sql,params);
    }

    @Override
    public User findByCode(String code) throws SQLException {
        String sql="select * from user where code=?";
        QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
        User user=qr.query(sql, new BeanHandler<>(User.class),code);
        return user;
    }

    @Override
    public User findByUsername(String username) throws SQLException {
        String sql="select * from user where username=?";
        QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
        return qr.query(sql, new BeanHandler<>(User.class),username);
    }

}